﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
namespace Framework.Common
{
    /// <summary>
    /// 常用验证
    /// </summary>
    public class CommonValidator
    {
        #region 验证邮箱
        /// <summary>
        /// 验证邮箱
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool isEmail(string source)
        {
            if (isNotNull(source))
            {
                bool flag = Regex.IsMatch(source, @"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", RegexOptions.IgnoreCase);

                return flag;
            }
            else
            { return false; }
        }
        #endregion

        #region 验证网址
        /// <summary>
        /// 验证网址
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool isUrl(string source)
        {
            if (isNotNull(source))
            {
                bool flag = Regex.IsMatch(source, @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&amp;%_\./-~-]*)?$", RegexOptions.IgnoreCase);

                return flag;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 验证日期
        /// <summary>
        /// 验证日期
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool isDateTime(string source)
        {
            if (isNotNull(source))
            {

                try
                {
                    DateTime time = Convert.ToDateTime(source);
                    return true;
                }
                catch
                {

                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 验证手机号
        /// <summary>
        /// 验证手机号
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool isMobile(string source)
        {
            if (isNotNull(source))
            {

                bool flag = Regex.IsMatch(source, @"^((\+86)|(\(\+86\)))?-?(13|14|15|18|17)[0-9]{9}$", RegexOptions.IgnoreCase);

                return flag;
            }
            else
            { return false; }
        }

        /// <summary>
        /// 验证座机号
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool isTelephone(string source)
        {
            if (isNotNull(source))
            {

                bool flag = Regex.IsMatch(source, @"^((\+86)|(\(\+86\)))?-?(((0[1,2]{1}\d{1})?-?\d{8})|((0[3-9]{1}\d{2})?-?\d{7,8}))$", RegexOptions.IgnoreCase);

                return flag;
            }
            else
            { return false; }
        }
        #endregion

        #region 整型验证
        /// <summary>
        /// 是不是Int型的
        /// </summary>
        /// <param name="source">验证的内容</param>
        /// <param name="error">返回错误信息，初始传字段名称</param>
        /// <returns></returns>
        public static bool isInt(string source)
        {
            if (isNotNull(source))
            {
                Regex regex = new Regex(@"^(-){0,1}\d+$");
                if (regex.Match(source).Success)
                {
                    if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
                    {

                        return false;

                    }
                    return true;
                }
                else
                {

                    return false;
                }
            }
            else
            { return false; }
        }
        #endregion

        #region 整型验证
        /// <summary>
        /// 是不是Money
        /// </summary>
        /// <param name="source">验证的内容</param>
        /// <param name="error">返回错误信息，初始传字段名称</param>
        /// <returns></returns>
        public static bool isMoney(string source)
        {
            bool flag = false;
            if (isNotNull(source))
            {
                Regex regex = new Regex(@"^\d{1,12}(?:\.\d{1,4})?$");
                if (regex.Match(source).Success)
                {

                    flag = true;
                }

            }
            return flag;

        }
        #endregion

        #region 身份证号码验证
        /// <summary>
        /// 身份证号码验证
        /// </summary>
        /// <param name="source">验证的内容</param>
        /// <param name="error">返回错误信息，初始传字段名称</param>
        /// <returns></returns>
        public static bool isPersonCode(string source)
        {
            bool flag = false;
            if (isNotNull(source))
            {
                Regex regex = new Regex(@"^([0-9]{17}[0-9X]{1})|([0-9]{15})$");
                if (regex.Match(source).Success)
                {
                    flag = true;
                }
            }
            return flag;
        }
        #endregion
        #region 看字符串的长度是不是在限定数之间 一个中文为两个字符
        /// <summary>
        /// 看字符串的长度是不是在限定数之间 一个中文为两个字符
        /// </summary>
        /// <param name="source">字符串</param>
        /// <param name="begin">大于等于</param>
        /// <param name="end">小于等于</param>
        /// <returns></returns>
        public static bool isLengthStr(string source, int begin = 1, int end = 1)
        {
            if (isNotNull(source))
            {

                int length = Regex.Replace(source, @"[^\x00-\xff]", "OK").Length;
                if (length < begin || length > end)
                {
                    return false;
                }
                return true;
            }
            else { return false; }
        }
        #endregion

        #region 邮政编码 6个数字
        /// <summary>
        /// 邮政编码 6个数字
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool isPostCode(string source)
        {
            bool flag = false;
            if (isNotNull(source))
            {
                flag = Regex.IsMatch(source, @"^\d{6}$", RegexOptions.IgnoreCase);

                return flag;
            }
            else
            {
                return flag;
            }
        }
        #endregion

        #region 验证是否为空
        public static bool isNotNull(string source)
        {
            if (string.IsNullOrWhiteSpace(source))
            {

                return false;
            }
            else { return true; }
        }
        #endregion


    }
}